package com.xin.jdbc_02;

import com.xin.jdbc_utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class psjdbc {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps_1=null;
        try {
            conn=DBUtil.getConnection();
            conn.setAutoCommit(false);
            ps_1= conn.prepareStatement("update ib set balance=? where id=?");
            ps_1.setInt(1,50);  //这里不理解，这里向？里面弄值也太麻烦了吧
            ps_1.setInt(2,1);
            int count=ps_1.executeUpdate();
            ps_1.setInt(1,50);
            ps_1.setInt(2,2);
            count+=ps_1.executeUpdate();
            System.out.println(count==2?"执行成功":"执行失败");
            conn.commit();

        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            if(ps_1!=null){
                try {
                    ps_1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
